package net.pws.common.security.filter;

import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.pws.common.security.WebUtils;


public class FilterInvocation {
    // ~ Instance fields
    // ================================================================================================
    
    private FilterChain chain;
    
    private ServletRequest request;
    
    private ServletResponse response;
    
    // ~ Constructors
    // ===================================================================================================
    
    public FilterInvocation(ServletRequest request,
                            ServletResponse response,
                            FilterChain chain) {
        if ((request == null) || (response == null) || (chain == null)) {
            throw new IllegalArgumentException("Cannot pass null values to constructor");
        }
        
        if (!(request instanceof HttpServletRequest)) {
            throw new IllegalArgumentException("Can only process HttpServletRequest");
        }
        
        if (!(response instanceof HttpServletResponse)) {
            throw new IllegalArgumentException("Can only process HttpServletResponse");
        }
        
        this.request = request;
        this.response = response;
        this.chain = chain;
    }
    
    // ~ Methods
    // ========================================================================================================
    
    public FilterChain getChain() {
        return chain;
    }
    
    /**
     * Indicates the URL that the user agent used for this request.
     * <P>
     * The returned URL does <b>not</b> reflect the port number determined from a
     * {@link org.acegisecurity.util.PortResolver}.
     * </p>
     * 
     * @return the full URL of this request
     */
    public String getFullRequestUrl() {
        return WebUtils.getFullRequestUrl(this);
    }
    
    public HttpServletRequest getHttpRequest() {
        return (HttpServletRequest) request;
    }
    
    public HttpServletResponse getHttpResponse() {
        return (HttpServletResponse) response;
    }
    
    public ServletRequest getRequest() {
        return request;
    }
    
    /**
     * Obtains the web application-specific fragment of the URL.
     * 
     * @return the URL, excluding any server name, context path or servlet path
     */
    public String getRequestUrl() {
        return WebUtils.getRequestUrl(this);
    }
    
    public ServletResponse getResponse() {
        return response;
    }
    
    public String toString() {
        return "FilterInvocation: URL: " + getRequestUrl();
    }
}
